package com.ddwl.order.dao;

import com.alibaba.fastjson.JSON;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.constant.OrderEnum;
import com.ddwl.common.dao.TkDao;
import com.ddwl.common.exception.GlobalException;
import com.ddwl.order.dao.mapper.OrderPayMapper;
import com.ddwl.order.dao.model.OrderPay;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 订单支付表(OrderPay)表数据库访问层
 *
 * @author makejava
 * @since 2020-06-02 14:48:13
 */
@Repository
@Slf4j
public class OrderPayDao extends TkDao<OrderPayMapper, OrderPay> {


    /**
     * 查询当前订单未支付记录
     * @param orderId
     * @return
     */
    public OrderPay queryNoPay(String orderId,Integer payType){
        Example example = new Example(OrderPay.class);
        example.and().andEqualTo("payType",payType)
                .andEqualTo("orderId",orderId)
                .andEqualTo("dflag", BaseEnum.DflagEnum.NOT_DELETE.getCode())
                .andNotEqualTo("tradeStatus", OrderEnum.PayStatus.SUCCEED.getCode());
        List<OrderPay> list = baseMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
    /**
     * 更新子订单状态
     * @param update
     * @return
     */
    public void updateOrderStatus(OrderPay update,String masterOrderId){
        OrderPay orderPay=new OrderPay();
        orderPay.setPayTimes(update.getPayTimes());
        orderPay.setTradeStatus(update.getTradeStatus());
        Example example = new Example(OrderPay.class);
        example.and().andEqualTo("masterOrderId",masterOrderId);
        int i = baseMapper.updateByExampleSelective(orderPay, example);
        if(i<1){
            log.error("订单状态更新异常 orderNo={}", JSON.toJSONString(masterOrderId));
            throw new GlobalException("订单状态更新异常");
        }
    }
    /**
     * 更新子订单付款状态
     * @param update
     * @return
     */
    public void updateCompletePayment(OrderPay update,String masterOrderId){
        Example example = new Example(OrderPay.class);
        example.and().andEqualTo("masterOrderId",masterOrderId);
        int i = baseMapper.updateByExampleSelective(update, example);
        if(i<1){
            log.error("订单状态更新异常 orderNo={}", JSON.toJSONString(masterOrderId));
            throw new GlobalException("订单状态更新异常");
        }
    }


    /**
     * 查询付款信息
     * @param orderId
     * @return
     */
    public OrderPay query(String orderId){
        Example example = new Example(OrderPay.class);
        example.and()
                .andEqualTo("orderId",orderId)
                .andEqualTo("dflag",BaseEnum.DflagEnum.NOT_DELETE.getCode())
                .andEqualTo("tradeStatus", OrderEnum.PayStatus.SUCCEED.getCode());
        example.setOrderByClause("gmt_created desc");
        List<OrderPay> list = baseMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}